package com.example.NeteaseCloudMusicApi.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import com.example.NeteaseCloudMusicApi.entity.LoginDTO;
import com.example.NeteaseCloudMusicApi.R;
import com.example.NeteaseCloudMusicApi.ui.APIService;
import com.example.NeteaseCloudMusicApi.ui.ConString;

import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class LoginActivity extends AppCompatActivity {

    private static final String TAG = "LoginActivity";
//    @BindView(R.id.btn_login_experience)
    private Button btn_login_experience;

    @OnClick(R.id.btn_login)
    void login() {
        Retrofit retrofit = new Retrofit.Builder()
                                    .baseUrl(ConString.BASE_URL)
                                    .addConverterFactory(GsonConverterFactory.create())
                                    .build();

        APIService retrofitApi = retrofit.create(APIService.class);
        Call<LoginDTO> userCall = retrofitApi.login("13770572452", "xjl021222");
        userCall.enqueue(new Callback<LoginDTO>() {
            @Override
            public void onResponse(Call<LoginDTO> call,
                                   Response<LoginDTO> response) {
                Log.i(TAG, "onResponse: " + "登录成功");
                LoginDTO loginDTO = response.body();
                Intent intent = new Intent();
                intent.putExtra("result", loginDTO);
                setResult(999, intent);
                finish();
            }

            @Override
            public void onFailure(Call<LoginDTO> call, Throwable t) {
                Log.i(TAG, "onFailure: " + "登录失败");
            }
        });

    }

//    @OnClick(R.id.btn_login_experience)
//    void loginExperience(View v){
//        Intent intent = new Intent(LoginActivity.this ,MainActivity.class);
//        startActivity(intent);
//    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //绑定初始化ButterKnife
        ButterKnife.bind(this);

        btn_login_experience = findViewById(R.id.btn_login_experience);
        btn_login_experience.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setContentView(R.layout.activity_main);
            }
        });

    }

}